<?php namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Services\Banner as sBanner;
use App\Models\Banner as mBanner;
use Illuminate\Http\Request;

class BannerController extends BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$banners = sBanner::getBanners();

		foreach ($banners as $banner) {
			$banner->img_url = mBanner::UPLOAD_PATH.$banner->img_url;
		}
		return $this->view('banner.index',compact('banners'));
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		return $this->view('banner.create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(Request $request)
	{	
		$data = $request->all();
		$data['img_url'] = $data['background_image_url'];
		mBanner::create($data);
		return $this->redirect('banner.index');		
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$banner = mBanner::where('id',$id)->first();
		unlink(public_path().'/uploads/'.$banner->img_url);
		mBanner::where('id',$id)->update(['status' => mBanner::DELETE_STATUS]);
		return $this->redirect('banner.index');
	}

}
